using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace JBORPG.Server
{
    public class Database
    {
        private String m_ConnectionString;
        private List<DatabaseConnection> m_Connections;
        public Database()
        {
            m_ConnectionString = Properties.Settings.Default.ConnectionString;
            m_Connections = new List<DatabaseConnection>();
        }

        private DatabaseConnection GetFreeConnection()
        {
            foreach (DatabaseConnection dc in m_Connections)
            {
                if (dc.ReaderIsFree)
                    return dc;
            }

            return new DatabaseConnection(m_ConnectionString);
        }

        public SqlDataReader Query(String sql, params SqlParameter[] parameters)
        {
            DatabaseConnection dc = GetFreeConnection();
            SqlCommand cmd = new SqlCommand(sql, dc.Connection);
            foreach (SqlParameter p in parameters)
            {
                cmd.Parameters.Add(p);
            }

            dc.Reader = cmd.ExecuteReader();
            return dc.Reader;
        }
    }
}
